<?php

/**
 * onlineSurvey actions.
 *
 * @package    iEasy
 * @subpackage onlineSurvey
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class onlineSurveyActions extends sfActions
{
    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $this->forward('default', 'module');
    }

    public function executeVote(sfWebRequest $request)
    {
        $type = $request->getParameter("type");
        $surveyId = $request->getParameter('surveyId');

        $survey = Doctrine_Core::getTable('OnlineSurvey')->find($surveyId);
        $yes_i = $survey->getYesCountI() != null ? $survey->getYesCountI() : 0;
        $no_i = $survey->getNoCountI() != null ? $survey->getNoCountI() : 0;
        switch ($type)
        {
            case "yes":
                $survey->setYesCountI($yes_i + 1);
                break;
            case "no":
                $survey->setNoCountI($no_i + 1);
                break;
        }
        $survey->save();
        if ($request->isXmlHttpRequest()) {
            return $this->renderComponent('onlineSurvey', 'onlineSurveyEntry');
        }
    }


    public function executeReaderSurvey(sfWebRequest $request)
    {
        $this->readerSurvey = Doctrine_Core::getTable('ReaderSurvey')->createQuery('s')
                ->addWhere('s.is_on_top=?', 1)->addOrderBy('s.start_date desc')->fetchOne();
    }

    public function executePostReaderSurvey(sfWebRequest $request)
    {
        //        $survey_id = $request->getParameter('survey_id');
        try {
            $ip = utility::getRequestIp($request);
            $userId = $this->getUser()->getAttribute('userId');
            $survey_id = $request->getParameter('survey_id');
            if ($userId == '' || $userId == null) $userId = 1;

            $feedback = new ReaderSurveyFeedback();
            $feedback->setSurveyId($survey_id);
            $feedback->setUserId($userId);
            $feedback->setUserIp($ip);
            $feedback->setCreateTime(date(Constants::$DEFAULT_DATETIME_FORMAT));
            $feedback->save();
            $feedbackId = $feedback->getId();

            $params = $request->getParameterHolder()->getAll();
            foreach ($params as $key => $val)
            {
                $tmp = preg_split('/_/', $key);
                if (count($tmp) > 1) {
                    //                    $survey_id = $tmp[0];
                    $question_id = $tmp[1];
                    $content = '';
                    if (is_array($val)) { //多选
                        $content = implode('', $val);
                    } else
                        $content = trim($val);
                    if (trim($content) != '') {
                        $feedbackDetail = new ReaderSurveyFeedbackDetail();
                        $feedbackDetail->fromArray(array(
                                                        'content' => $content,
                                                        'question_id' => $question_id,
                                                        'form_id' => $feedbackId,
                                                        'create_time' => date(Constants::$DEFAULT_DATETIME_FORMAT),
                                                   ));
                        $feedbackDetail->save();
                    }
                    //                    $feedbackDetail = new ReaderSurveyFeedbackDetail();
                    //                    $feedbackDetail->fromArray(array(
                    //                                                ''
                    //                                               ));

                    //                    $feedback = new ReaderSurveyFeedback();
                    //                    $feedback->fromArray(array(
                    //                                              'survey_id' => $survey_id,
                    //                                              'question_id' => $question_id,
                    //                                              'content' => $content,
                    //                                              'user_id' => $userId,
                    //                                              'user_ip' => $ip,
                    //                                              'create_time' => date(Constants::$DEFAULT_DATETIME_FORMAT),
                    //                                         ));
                    //                    $feedback->save();
                }

            }


            if ($request->isXmlHttpRequest()) {
                $readerSurvey = Doctrine_Core::getTable('ReaderSurvey')->createQuery('s')
                        ->addWhere('s.is_on_top=?', 1)->addOrderBy('s.start_date desc')->fetchOne();
                return $this->renderPartial('readerSurvey', array('readerSurvey' => $readerSurvey));
            }
        } catch (Exception $e) {
        }
    }
}
